<?php

namespace app\platform\controller\admin;

use app\BaseController;

use app\common\logic\AppLogic;

use app\common\logic\DataLogic;

use app\common\model\account\AccountModel;

use app\common\model\ActivationCodeModel;

use app\common\model\AppModel;

use app\common\model\SmsTplModel;



class Appliance extends BaseController{

    protected $name='应用';



    public function index()

    {

        $status=$this->request->param('status/d',0);

        $app_type=$this->request->param('app_type/d');

        $where=[

            ['is_delete','=',0],

            ['status','=',$status]

        ];

        if(!empty($app_type)){

            $where[]=['app_type','=',$app_type];

        }

        $query=new AppModel();

        $order=['sort'=>'desc','add_time'=>'desc'];

        $append=['add_time_format'];

        $data=DataLogic::getDataList($query,$where,$order,'*',$this->request->limit,$this->request->page,$append);

        return $this->success($data);

    }



    public function show()

    {

        $app_id=$this->request->param('app_id/d',0);

        $AppLogic= new AppLogic();

        $app_type=$AppLogic->getAppTypeList();

        if($app_id==0){

            return $this->success(['app_type'=>$app_type]);

        }

        if(null === $app = AppModel::find($app_id)){

            return $this->error($this->name.'不存在');

        }



        if($app->is_delete !=0){

            return $this->error($this->name.'不存在');

        }



//        $white_list_data=AccountModel::where([

//            ['is_delete','=',0],

//            ['account_id','in',$app->white_list]

//        ])->field('account_id,username,mobile')->select()->toArray();



        $detail=[

            'name'=>$app->name,

            'iden'=>$app->iden,

            'logo'=>$app->logo,

            'type'=>$app->type,

            'sort'=>$app->sort,

            'is_tui'=>$app->is_tui,

            'white_list'=>$app->white_list,

//            'white_list_data'=>$white_list_data,

            'production_address'=>$app->production_address,

            'development_address'=>$app->development_address,

            'trial_days'=>$app->trial_days,

            'sku'=>$app->sku ?? [],

            'scene'=>$app->scene,

        ];

        return $this->success(['app_type'=>$app_type,'detail'=>$detail]);

    }



    public function getAccountData()

    {

        $white_list=$this->request->param('white_list/a',[]);

        $where=[

            ['is_delete','=',0],

            ['account_id','in',$white_list]

        ];

        $data=AccountModel::where($where)->order(['add_time'=>'desc'])->select()->toArray();



        return $this->success(['data'=>$data]);

    }



    public function save()

    {

        $app_id=$this->request->param('app_id/d',0);

        $name=$this->request->param('name');

        $iden = $this->request->param('iden');

        $logo=$this->request->param('logo');

        $type=$this->request->param('type/d');

        $sort=$this->request->param('sort/d',999);

        $is_tui=$this->request->param('is_tui/d',0);

        $production_address=$this->request->param('production_address');

        $development_address=$this->request->param('development_address');

        $white_list_data=$this->request->param('white_list_data','','security_json_or_array');

        $trial_days=$this->request->param('trial_days/d',0);

        $sku=$this->request->param('sku','','security_json_or_array');

        $scene=$this->request->param('scene');

        if(empty($name) || mb_strlen($name)>10){

            return $this->error('名称必须填写且长度不能多于10');

        }

        if(empty($iden)){

            return $this->error('请输入标识');

        }

        if(preg_match('/[^A-Za-z]+/', $iden)){

            return $this->error('标识请输入全字母');

        }

        if(empty($logo)){

            return $this->error('请上传logo');

        }

        if(empty($type)){

            return $this->error('请选择分类');

        }

        if(empty($sort) || $sort<=0){

            return $this->error('请填写排序，且排序需要大于0');

        }

        if(!in_array($is_tui,[0,1])){

            return $this->error('参数错误');

        }

        if(empty($production_address)){

            return $this->error('请输入生产地址');

        }

        if(empty($development_address)){

            return $this->error('请输入开发地址');

        }

        if(empty($white_list_data) || !is_array($white_list_data)){

            return $this->error('请选择开发白名单');

        }

        if(empty($trial_days)){

            return $this->error('请输入试用天数');

        }

        if(empty($sku) || !is_array($sku)){

            return $this->error('请输入规格');

        }

        if(empty($scene)){

            return $this->error('请填写试用场景');

        }



        //处理白名单

        $white_list=array_column($white_list_data, 'account_id') ?? [];



        //处理规格

        foreach ($sku as $k => $v){

            if(!in_array($v['unit'],[1,2,3])){

                return $this->error('规格的第'.$k.'列里的时长单位错误');

            }

            if(empty($v['num']) || $v['num']<=0){

                return $this->error('规格的第'.$k.'列里的时长不能为空且不能小于等于0');

            }

            if(empty($v['base_price']) || $v['base_price']<=0){

                return $this->error('规格的第'.$k.'列里的基本价格不能为空且不能小于等于0');

            }

            if(empty($v['price']) || $v['price']<=0){

                return $this->error('规格的第'.$k.'列里的价格不能为空且不能小于等于0');

            }

            if($v['price']>$v['base_price']){

                return $this->error('规格的第'.$k.'列里的价格不能大于基本价格');

            }

            $sku[$k]['num']=(int)$v['num'];

            $sku[$k]['unit']=(int)$v['unit'];

            $sku[$k]['base_price']=(float)$v['base_price'];

            $sku[$k]['price']=(float)$v['price'];

            $sku[$k]['discount']=round($v['price']*10/$v['base_price'],2);

        }

         if($app_id!=0){

             if(null === $app = AppModel::find($app_id)){

                 return $this->error($this->name.'不存在');

             }

             if($app->is_delete !=0){

                 return $this->error($this->name.'不存在');

             }

             $pre_logo=$app->logo;

         }else{

             $app = new AppModel();

             $pre_logo=null;

         }



         $app->name=$name;

         $app->iden=$iden;

         $app->logo=$logo;

         $app->type=$type;

         $app->sort=$sort;

         $app->is_tui=$is_tui;

         $app->white_list=$white_list;

         $app->production_address=$production_address;

         $app->development_address=$development_address;

         $app->trial_days=$trial_days;

         $app->sku=$sku;

         $app->scene=$scene;

         $app->save();

         imgReplaceAct($logo,$pre_logo);

         return $this->success('保存成功');

    }



    public function getActivationCodeList()

    {

        $where=[

            ['is_delete','=',0]

        ];

        $activation_code_model= new ActivationCodeModel();

        $activation_code_model = $activation_code_model->with(['app']);

        $order=['status'=>'asc','expire_time'=>'asc','add_time'=>'desc','activation_code_id'=>'desc'];

        $append=['add_time_format','expire_time_format'];

        $data=DataLogic::getDataList($activation_code_model,$where,$order,'*',$this->request->limit,$this->request->page,$append);

        return $this->success($data);



    }



    public function generateActivationCode()

    {

        $app_id=$this->request->param('app_id/d',0);

        if(null === $app = AppModel::find($app_id)){

            return $this->error($this->name.'不存在');

        }



        if($app->is_delete !=0){

            return $this->error($this->name.'不存在');

        }



        $unit=$this->request->param('unit/d',0);

        $days=$this->request->param('days/d',0);

        $valid_days = $this->request->param('valid_days/d',0);

        $price = $this->request->param('price/f',0);

        if(empty($unit) || $unit <=0) return $this->error('请填写个数');

        if(empty($days) || $days <=0) return $this->error('请填写天数');

        if(empty($valid_days) || $valid_days <=0) return $this->error('请输入作用天数');

        if(empty($price) || $price <=0) return $this->error('请输入价格');

        if($unit>50) return $this->error('不能超过50个');



        $activation_code_data=[];

        for($i=1;$i<=$unit;$i++){

            $activation_code_data[]=[

                'app_id'=>$app_id,

                'valid_days'=>$valid_days,

                'price'=>$price,

                'activation_code'=>md5($this->request->time().round(100000,999999).$i.$app_id.$unit.$days.$valid_days),

                'expire_time'=>$this->request->time() + $days * 86400,

            ];

        }



        $activation_code_model= new ActivationCodeModel();

        $activation_code_model->saveAll($activation_code_data);

        return $this->success('生成成功');



    }



    public function delActivationCode()

    {

        $activation_code_id=$this->request->param('activation_code_id/d',0);

        if(null === $activation_code = ActivationCodeModel::find($activation_code_id)){

            return $this->error('激活码不存在');

        }

        if($activation_code->is_delete != 0) return $this->error('激活码不存在');

        $activation_code->is_delete = 1;

        $activation_code->save();

        return $this->success('删除成功');

    }







}

